﻿using Microsoft.Practices.ObjectBuilder2;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Data;
using TciNewsReader_2014_04.Infrastructure;
using TciNewsReader_2014_04.Interfaces;
using TciNewsReader_2014_04.Model;
using TciNewsReader_2014_04.VmHelpers;
namespace TciNewsReader_2014_04
{
    public partial class ViewModel
    {
        private static readonly object Locker = new object();
        private readonly IConfirmations _confirmations;
        private readonly IErrorReport _errorReportInitializer;
        private readonly IFeedCrud _feedCrudInitializer;
        private readonly TaskRunner _taskRunner;
        private readonly IExceptionHandler _exceptionHandler;
        private readonly IHelpAbout _helpAboutInitializer;
        private readonly IWindowInteraction _includeExcludeInitializer;
        private readonly ListCollectionView _itemWrappersListCollectionView;
        private readonly ListCollectionView _feedInfosListCollectionView;
        private readonly IPersistenceManager _persistenceManager;
        public Stats VmStats { get; private set; }
        public ObservableCollection<FeedInfo> FeedInfos { get; private set; }
        public ObservableCollection<ItemWrapper> ItemWrappers { get; set; }
        private ItemWrapper _masterViewSelectedItem;
        public ItemWrapper MasterViewSelectedItem
        {
            get { return _masterViewSelectedItem; }
            set
            {
                if (value != _masterViewSelectedItem)
                {
                    ItemWrappers.Where(q => q.ItemStatus == ItemStatus.Current).ForEach(q =>
                    {
                        q.ItemStatus = ItemStatus.Read;
                        ItemWrapperHistoryManager.SaveReadItem(q);
                        FilteringHandler(q);
                        q.OwnerInfo.RaiseNotifyOnCounts();
                    });
                    _masterViewSelectedItem = value;
                    if (_masterViewSelectedItem == null)
                    {
                        CurrentItemTitle = String.Empty;
                        CurrentItemContent = null;
                        return;
                    }
                    CurrentItemTitle = _masterViewSelectedItem.ItemTitle;
                    CurrentItemContent = _masterViewSelectedItem.ItemSummary;
                    _masterViewSelectedItem.ItemStatus = ItemStatus.Current;
                    FilteringHandler(_masterViewSelectedItem);
                   UpdateCountsStatus();
                    OnPropertyChanged("MasterViewSelectedItem");
                 }
            }
        }
        private string _currentItemContent;
        public string CurrentItemContent
        {// displays in the browser control
            [DebuggerStepThrough]
            get { return _currentItemContent; }
            [DebuggerStepThrough]
            set
            {
                if (value != _currentItemContent)
                {
                    _currentItemContent = value;
                    OnPropertyChanged("CurrentItemContent");
                }
            }
        }
        private string _currentItemTitle;
        public string CurrentItemTitle
        {// displays in the title control
            [DebuggerStepThrough]
            get { return _currentItemTitle; }
            [DebuggerStepThrough]
            set
            {
                if (value != _currentItemTitle)
                {
                    _currentItemTitle = value;
                    OnPropertyChanged("CurrentItemTitle");
                }
            }
        }
        private string _countsStatus;
        public string CountsStatus
        {// displays on the status bar
            [DebuggerStepThrough]
            get { return _countsStatus; }
            [DebuggerStepThrough]
            set
            {
                if (value != _countsStatus)
                {
                    _countsStatus = value;
                    OnPropertyChanged("CountsStatus");
                }
            }
        }
        private bool _hideReadItems = true;
        public bool HideReadItems
        {// binded to the check box
            [DebuggerStepThrough]
            get { return _hideReadItems; }
            [DebuggerStepThrough]
            set
            {
                if (value != _hideReadItems)
                {
                    _hideReadItems = value;
                    OnPropertyChanged("HideReadItems");
                    ItemWrappers.ForEach(FilteringHandler);
                    FeedInfos.ForEach(q=>q.RaiseNotifyOnCounts());
                    UpdateCountsStatus();
                }
            }
        }
        private bool _isBusy;
        public bool IsBusy
        {// controls visibility of the busy indicator only!
            [DebuggerStepThrough]
            get { return _isBusy; }
            [DebuggerStepThrough]
            set
            {
                if (value != _isBusy)
                {
                    _isBusy = value;
                    OnPropertyChanged("IsBusy");
                    if (!_isBusy)
                    {
                        FeedInfos.ForEach(q => q.RaiseNotifyOnCounts());
                    }
                }
            }
        }
        private bool _isReadyForInteraction;
        public bool IsReadyForInteraction
        {// controls whether or not the app is ready for clicks
            [DebuggerStepThrough]
            get { return _isReadyForInteraction; }
            [DebuggerStepThrough]
            set
            {
                if (value != _isReadyForInteraction)
                {
                    _isReadyForInteraction = value;
                    OnPropertyChanged("IsReadyForInteraction");
                    if (value)
                    {
                        NotifyCommandChanges();
                    }
                }
            }
        }
    }
}
